/* -*- mode: c++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*- */

#ifndef _TCPCLIENT_H_
#define _TCPCLIENT_H_

#include <string>

#include <utility.h>

class TcpClient {
public:
    std::string                 ip;

    int                         port;

    int                         fd;

    bool                        conn_p;

    Uint64                      conn_ticks;

    int                         conn_cnt;

    TcpClient()
        : port(-1)
        , fd(-1)
        , conn_p(false)
        , conn_ticks(0)
        , conn_cnt(0)
    {
    }

    TcpClient(const char *i, int p)
        : ip(i)
        , port(p)
        , fd(-1)
        , conn_p(false)
        , conn_ticks(0)
    {
        connect_to();
    }

    ~TcpClient() { close_conn(); }

    void set_ip(const char *i) { ip = i; }

    void set_port(int p) { port = p; }

    bool is_connected() const { return conn_p; }

    bool connect_to();

    void close_conn();

    int get_fd() const { return fd; }

    int get_conn_cnt() const { return conn_cnt; }

    Uint64 get_conn_ticks() const { return conn_ticks; }

    bool need_conn(int interval_us)
    {
        if (conn_cnt == 0)
            return true;

        return (int (get_ticks() - conn_ticks) >= interval_us);
    }
};

#endif /* _TCPCLIENT_H_ */
